<?php

namespace Admin\Controller;
use Org\BMap;

class DemoController extends AdminController {

    public function demo() {
        $rest = new \Org\Util\Rest();
        $req = $rest->get("classes/GameScore");
        dump($req);
        $result = $req->body->results;
        //echo $result;
        for ($i = 0; $i < 3; $i++) {
            dump($result[$i]->name);
        }


        //$req=$rest->delete("classes/hello/5554a914e4b0a343c5b368fb");
        $this->display();
    }

    public function demo1() {

        //新增加及修改
        $av = new \Org\Util\AVObject("Person");
        $av->set("name", "李明博");
        $av->set("age", 27);
        $av->set("booltest", FALSE);
        $av->save();
        $av->set("age", 80);
        $av->save();

        //获取单个对象修改后保存
        $avqure = new \Org\Util\AVQuery("Person");
        $av = $avqure->get("5557430be4b08467611a14e3");
        $av->set("name", "曹梦龙888");
        $av->save();

        //删除单个对象
        $avqure = new \Org\Util\AVQuery("Person");
        $av = $avqure->get("5557430be4b08467611a14e3");
        $av->destroy();


        //获取多个对象列表
        $avqure = new \Org\Util\AVQuery("Person");
        $avlist = $avqure->getList();
        dump($avlist);
    }

    public function index() {
        $name = I('name');
        $map['name'] = array('like', '%' . (string) $name . '%');
        $list = $this->lists('gamescore', $map);
        int_to_string($list);
        //dump($list);
        $this->assign('_list', $list);
        $this->meta_title = '数据列表';
        $this->display();
    }

    public function add($score = null, $playerName = null, $cheatMode = null) {
        if (IS_POST) {
            $hashcode = md5(NOW_TIME);
            $gamescoredata = array('score' => $score, 'playerName' => $playerName, 'cheatMode' => $cheatMode = 'on' ? 1 : 0, 'hashCode' => $hashcode, 'status' => 1);
            $GameScore = D('gamescore'); //开启事务
            $GameScore->startTrans();

            $resultID = $GameScore->add($gamescoredata);
            if (!$resultID) {
                $this->error("添加失败！");
            } else {
                $json_arr = array('masterId' => (int) $resultID, 'score' => (int) $score, 'playerName' => $playerName, 'cheatMode' => $cheatMode == 'on' ? 1 : 0, 'hashCode' => $hashcode, 'status' => 1);
                $php_json = json_encode($json_arr);
                $rest = new \Org\Util\Rest();
                $result = $rest->add("GameScore", $php_json);
                if ($result->body->objectId) {
                    $GameScore->where(array('id' => $resultID))->setField('objectId', $result->body->objectId);
                    $GameScore->commit();
                    $this->success('添加成功！', U('index'));
                } else {
                    $GameScore->rollback();
                    $this->error("添加失败！");
                }
            }
        } else {
            $this->display();
        }
    }

    public function edit($objectId = null, $score = null, $playerName = null, $cheatMode = null) {
        $objectId = I('objectId');
        if (IS_POST) {
            $hashcode = md5(NOW_TIME);
            $gamescoredata = array('score' => (int) $score, 'playerName' => $playerName, 'cheatMode' => $cheatMode = 'on' ? 1 : 0, 'hashCode' => $hashcode);
            $GameScore = D('gamescore'); //开启事务
            $GameScore->startTrans();

            if (!$GameScore->where(array('objectId' => $objectId))->save($gamescoredata)) {
                $this->error("修改失败！");
            } else {
                $php_json = json_encode($gamescoredata);
                $rest = new \Org\Util\Rest();
                $result = $rest->put("GameScore", $objectId, $php_json);
                if ($result->body->objectId) {
                    $GameScore->commit();
                    $this->success('修改成功！', U('index'));
                } else {
                    $GameScore->rollback();
                    $this->error("修改失败！");
                }
            }
        } else {

            $data = M('gamescore')->where(array('objectId' => $objectId))->find();

            $this->wdata = $data;
            $this->display();
        }
    }

    //删除数据
    public function del($objectId = '') {
        if ($objectId) {
            $hashcode = md5(NOW_TIME);
            $gamescoredata = array('status' => -1, 'hashCode' => $hashcode);
            $GameScore = D('gamescore'); //开启事务
            $GameScore->startTrans();
            $reuslt = $GameScore->where(array('objectId' => $objectId))->save($gamescoredata);
            if ($reuslt) {
                $php_json = json_encode($gamescoredata);
                $rest = new \Org\Util\Rest();
                $result = $rest->put("GameScore", $objectId, $php_json);
                if ($result->body->objectId) {
                    $GameScore->commit();
                    $this->success('删除修改成功！', U('index'));
                } else {
                    $GameScore->rollback();
                    $this->error("删除修改失败！");
                }
            } else {
                $this->error("删除失败！");
            }
        }
    }

    ///file 上传
    public function file($filename = "", $context = "") {
        if (IS_POST) {

            // 上传文件 
            $setting = array(
                'mimes' => '', //允许上传的文件MiMe类型
                'maxSize' => 2 * 1024 * 1024, //上传的文件大小限制 (0-不做限制)
                'exts' => 'jpg,gif,png,jpeg', //允许上传的文件后缀
            );

            /* 调用文件上传组件上传文件 */
            /*             * 修改开始* */
            $pic_driver = C('PICTURE_UPLOAD_DRIVER');
            $upload = new \Think\Upload($setting, C('PICTURE_UPLOAD_DRIVER'), C("UPLOAD_{$pic_driver}_CONFIG"));
            $info = $upload->upload();
            if (!$info) {// 上传错误提示错误信息
                $this->error($upload->getError());
            } else {// 上传成功
                dump($info);
                dump($filename);
                dump($context);
                //$this->success('上传成功！');
            }
        } else {
            $this->display();
        }
    }

    //leancloud 
    public function leancloud($name = null) {
        $where = '{"status":1}';
        if (!is_null($name)) {
            $where = '{"status":1,"playerName":"' . $name . '"}';
        }


        $list = $this->listsLeanCloud("GameScore", $where);

        int_to_string($list);
        //dump($list);
        $this->assign('_list', $list);
        $this->display();
    }

    //云直连添加
    public function avadd($score = null, $playerName = null, $cheatMode = null) {
        if (IS_POST) {
            $av = new \Org\Util\AVObject("GameScore");
            $av->set("playerName", $playerName);
            $av->set("score", (int) $score);
            $av->set('status', 1);
            $av->set("cheatMode", $cheatMode == 'on' ? 1 : 0);
            $result = $av->save();
            if ($result['objectId']) {

                $this->success('添加成功！', U('leancloud'));
            } else {

                $this->error("添加失败！");
            }
        } else {
            $this->display();
        }
    }

    public function avedit($objectId = null, $score = null, $playerName = null, $cheatMode = null) {

        if (IS_POST) {
            $avqure = new \Org\Util\AVQuery("GameScore");
            $av = $avqure->get($objectId);
            if ($av->objectId) {
                $av->set("playerName", $playerName);
                $av->set("score", (int) $score);
                $av->set("cheatMode", $cheatMode == "on" ? 1 : 0);
                $result = $av->save();
                if ($result['objectId']) {
                    $this->success('修改成功！', U('leancloud'));
                } else {
                    $this->error("修改失败！");
                }
            }
        } else {
            $avqure = new \Org\Util\AVQuery("GameScore");
            $av2 = $avqure->get($objectId);
            $this->wdata = $av2->getArray();
            $this->display();
        }
    }

    //云删除
    public function avdel($objectId = '') {
        if ($objectId) {
            $avqure = new \Org\Util\AVQuery("GameScore");
            $av = $avqure->get($objectId);
            $av->set('status', -1);
            $result = $av->save();
            if ($result['objectId']) {
                $this->success('删除成功！', U('index'));
            } else {
                $this->error("删除失败！");
            }
        }
    }
    
    
    //判断百度坐标是否在我们指定的范围内
    public function point($lng=null,$lat=null){
        if(IS_POST){
            //创建点
            $point =new BMap\Point((float)$lng,(float)$lat);
            //创建一个多边形数据组
             $point1 =new BMap\Point(116.397908,39.929184);
             $point2 =new BMap\Point(116.408437,39.929544);
             $point3 =new BMap\Point(116.408868,39.919667);
             $point4 =new BMap\Point(116.398411,39.919197);
             $pointArray= array();
             $pointArray[]=$point1;
             $pointArray[]=$point2;
             $pointArray[]=$point3;
             $pointArray[]=$point4;
             $GeoUtils=new BMap\GeoUtils();
             $result=$GeoUtils->isPointInPolygon($point, $pointArray);
             
             dump($result);
             
            $this->display();
        }else{
            $this->display();
        }
        
    }

}
